package com.briup.jz.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.jz.bean.BaseLog;
import com.briup.jz.bean.BaseLogExample;
import com.briup.jz.bean.BaseLog;
import com.briup.jz.dao.BaseLogMapper;
import com.briup.jz.service.IBaseLogService;
import com.briup.jz.utils.CustomerException;

@Service
public class BaseLogServiceImpl implements IBaseLogService {
	
	@Autowired
	private BaseLogMapper baselogMapper;

	@Override
	public void saveOrUpdate(BaseLog baselog) throws CustomerException {
		// TODO Auto-generated method stub
		if(baselog.getId()!=null) {
			baselogMapper.updateByPrimaryKey(baselog);
		} else {
			// 判断是否有同名的分类，如果有抛出异常
		BaseLogExample example = new BaseLogExample();
			example.createCriteria().andRealnameEqualTo(baselog.getRealname());
			List<BaseLog> list = baselogMapper.selectByExample(example);
			if(list.size()>0) {
				throw new CustomerException("同名的realname已存在");
			}
		
			baselogMapper.insert(baselog);
		}
		}
		
	

	@Override
	public List<BaseLog> query(String realname) {
		// TODO Auto-generated method stub
		BaseLogExample example = new BaseLogExample();
	if( realname!=null){
			example.createCriteria().andRealnameLike("%"+realname+"%");
		}
		

		List<BaseLog> list = baselogMapper.selectByExample(example);
		return list;
	}



	public void deleteById(Long id) throws CustomerException {
		// TODO Auto-generated method stub
		BaseLog baselog = baselogMapper.selectByPrimaryKey(id);
		if(baselog == null) {
			throw new CustomerException("要删除的日志信息不存在");
		}
		baselogMapper.deleteByPrimaryKey(id);
		
	}
		
	}

